package com.qr.controller.wmywApi;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RestController;
import cn.krui.helper.bean.AppResult;
import cn.krui.helper.bean.DataArray;
import cn.krui.helper.filter.JSON;
import com.qr.controller.WmywBaseController;
import com.qr.bean.wmyw.QrOrderTakeoutGoods;
import com.qr.bean.wmyw.QrOrderTakeoutGoodsView;
import com.qr.service.wmyw.QrOrderTakeoutGoodsService;
import java.util.List;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.ObjectError;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.krui.helper.tools.DateTimeUtils;
/**
 * @author: zc
 * email: zc8023zs@163.com
 * create_time: 2020/8/13 10:24:36
 */
@Api(tags = "qr_order_takeout_goodsApi", description = "门店商品Api")
@RestController
@RequestMapping("/wmywApi/qr_order_takeout_goods")
public class QrOrderTakeoutGoodsApiController extends WmywBaseController {

    @Autowired
    private QrOrderTakeoutGoodsService qrOrderTakeoutGoodsService;
    
    @PostMapping(value = "/add")
	@ApiOperation(value = "门店商品添加", httpMethod = "POST")
    public AppResult<Integer> add(@Valid QrOrderTakeoutGoods model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                //主键不用判断
                if("order_goods_id".equals(columnname)){
					continue;
				}
                //版本字段不用判断
                if("version_id".equals(columnname)){
					continue;
				}
                app.no(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = qrOrderTakeoutGoodsService.add(model);
        if(i>0) {
			i = model.getOrder_goods_id();
		}
        app.setDataSingle(i);
        app.ok("添加成功");
        return app;
    }
    
    @PostMapping(value = "/update")
	@ApiOperation(value = "门店商品修改", httpMethod = "POST")
    public AppResult<Integer> update(@Valid QrOrderTakeoutGoods model,BindingResult bindingResult){
        AppResult<Integer> app = new AppResult<Integer>();
        if (bindingResult.hasErrors()) {
            List<ObjectError> oerr = bindingResult.getAllErrors();
            for (ObjectError objectError : oerr) {
                DefaultMessageSourceResolvable d = (DefaultMessageSourceResolvable)objectError.getArguments()[0];
                String columnname = d.getDefaultMessage();
                //版本字段不用判断
                if(columnname.equals("version_id")){
					continue;
				}
                app.no(bindingResult.getAllErrors().get(0).getDefaultMessage());
                return app;
            }
        }
        Integer i = qrOrderTakeoutGoodsService.update(model);
        app.ok("修改成功");
        app.setDataSingle(i);
        return app;
    }
    
    @PostMapping(value = "/del")
	@ApiOperation(value = "门店商品删除", httpMethod = "POST")
    public AppResult<Integer> del(Integer id){
        AppResult<Integer> app = new AppResult<Integer>();
        Integer i = qrOrderTakeoutGoodsService.del(id);
        app.ok("删除成功");
        app.setDataSingle(i);
        return app;
    }

    @GetMapping(value = "/info")
	@ApiOperation(value = "门店商品详情", httpMethod = "GET")
    //只支持简单对象序列化过滤，自行添加需要输出的字段
    @JSON(type = QrOrderTakeoutGoodsView.class, include = "order_goods_id")
    public AppResult<QrOrderTakeoutGoodsView> info(Integer id){
        AppResult<QrOrderTakeoutGoodsView> app = new AppResult<QrOrderTakeoutGoodsView>();
        QrOrderTakeoutGoodsView i = qrOrderTakeoutGoodsService.info(id);
        app.setDataSingle(i);
        app.ok("查询成功");
        return app;
    }

    @PostMapping(value = "/select")
	@ApiOperation(value = "门店商品列表", httpMethod = "POST")
    //只支持简单对象序列化过滤，自行添加需要输出的字段
    @JSON(type = QrOrderTakeoutGoodsView.class, include = "order_goods_id")
    public AppResult<QrOrderTakeoutGoodsView> select(
        ){
        AppResult<QrOrderTakeoutGoodsView> app = new AppResult<QrOrderTakeoutGoodsView>();
        QrOrderTakeoutGoodsView where = new QrOrderTakeoutGoodsView();
        List<QrOrderTakeoutGoodsView> i = qrOrderTakeoutGoodsService.select(where);
        DataArray<QrOrderTakeoutGoodsView> da=new DataArray<QrOrderTakeoutGoodsView>();
        da.setDataList(i);
        app.setDataArray(da);
        app.ok("查询成功");
        return app;
    }

    @PostMapping(value = "/selectpage")
    @ApiOperation(value = "门店商品列表分页", httpMethod = "POST")
    //只支持简单对象序列化过滤，自行添加需要输出的字段
    @JSON(type = QrOrderTakeoutGoodsView.class, include = "order_goods_id")
    public AppResult<QrOrderTakeoutGoodsView> selectpage(
            @ApiParam(value = "页", required = true) @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @ApiParam(value = "条", required = true) @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        AppResult<QrOrderTakeoutGoodsView> app = new AppResult<QrOrderTakeoutGoodsView>();
        QrOrderTakeoutGoodsView where = new QrOrderTakeoutGoodsView();
        IPage<QrOrderTakeoutGoodsView> pg = new Page<QrOrderTakeoutGoodsView>();
        pg.setCurrent(page); 
        pg.setSize(size);
        IPage<QrOrderTakeoutGoodsView> i = qrOrderTakeoutGoodsService.select(pg,where);
        int total = qrOrderTakeoutGoodsService.selectCount(where);
        DataArray<QrOrderTakeoutGoodsView> da = new DataArray<QrOrderTakeoutGoodsView>();
        da.setPageCount((total/size)+1);
        da.setDataCount(total);
        da.setPageIndex(page);
        da.setPageSize(size);
        da.setDataList(i.getRecords());
        app.setDataArray(da);
        app.ok("查询成功");
        return app;
    }

}